package com.roocon.design_pattern.chapter03_readwrite_lock;

public class WriteWorker extends Thread {

  private final ShareData data;
  private final String filler;

  private int index ;

  public WriteWorker(ShareData data,String filler) {
    this.data = data;
    this.filler = filler;
  }

  @Override public void run() {
    try {
      while (true) {
        data.write(nextChar());
        Thread.sleep(1000);
      }
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  private char nextChar() {
    char ch = filler.charAt(index);
    index ++;
    if (index >= filler.length()) {
      index = 0;
    }
    return ch;
  }

}
